

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Set;
import java.util.Vector;

public class U {


	
	public static int getIndex(String[] sa, String s) {
		for (int i = 0; i < sa.length; i++) {
			if (sa[i].equals(s))
				return i;
		}
		return -1;
	}

	public static double minOr0(double[] da) {
		double res = 0;
		for (int i = 0; i < da.length; i++) {
			if (da[i] < res)
				res = da[i];
		}
		return res;
	}




	public static boolean isIn(int[] ia, int content) {
		for (int i = 0; i < ia.length; i++) {
			if (content == ia[i])
				return true;
		}
		return false;
	}

	public static BufferedReader getReader(String file)
			throws FileNotFoundException {
		return new BufferedReader(new FileReader(file));
	}



	static boolean isDigit(String value) {
		try {
			Double.parseDouble(value);
			return true;
		} catch (NumberFormatException e) {
			return false;
		}
	}

	static double truncation(double d) {
		int _d = (int) (d * 100);
		return ((double) _d) / 100;
	}

	public static BufferedWriter newWriter(String file){
		try {
			return new BufferedWriter(new FileWriter(file));
		} catch (IOException e) {
			System.err.println("fail to write file "+file);
			e.printStackTrace();
		}
		return null;
	}
	

	public static void add(HashMap<String, Integer> m, String s) {
		int i = 0;
		if (null != m.get(s))
			i = m.get(s);
		m.put(s, i + 1);
	}

	public static void add(Hashtable<String, Integer> m, String s) {
		int i = 0;
		if (null != m.get(s))
			i = m.get(s);
		m.put(s, i + 1);
	}

	public static double getExpectation(Vector<Double> v) {
		double result = 0;
		for (int i = 0; i < v.size(); i++) {
			result += v.get(i);
		}
		return result / v.size();
	}

	public static double getSD(Vector<Double> v) {
		double e = getExpectation(v);
		double result = 0;
		for (int i = 0; i < v.size(); i++) {
			result += (v.get(i) - e) * (v.get(i) - e);
		}
		return Math.sqrt(result / (v.size() - 1));
	}

	public static double relatedCoefficient(Vector<Double> v, Vector<Double> v2) {

		double res = 0;
		double e = getExpectation(v);
		double e2 = getExpectation(v2);
		for (int i = 0; i < v.size(); i++) {
			res += (v.get(i) - e) * (v2.get(i) - e2);
		}
		return res / getSD(v) / getSD(v2) / v.size();
	}

	public static int tryGet(HashMap<String, Integer> hm, String s) {
		if (null != hm.get(s))
			return hm.get(s);
		return 0;
	}

	public static int hashtableTryGet(Hashtable<String, Integer> hm, String s) {
		if (null != hm.get(s))
			return hm.get(s);
		return 0;
	}

	@SuppressWarnings("unchecked")
	public static Object getProperty(Object owner, String fieldName)
			throws Exception {
		Class ownerClass = owner.getClass();

		Field field = ownerClass.getField(fieldName);

		Object property = field.get(owner);

		return property;
	}

	public static double tryGetD(Hashtable<String, Double> ht, String key) {
		if (null != ht.get(key)) {
			return ht.get(key);
		}
		return 0;
	}
	

	public static float tryGetF(HashMap<String, Float> ht, String key) {
		if (null != ht.get(key)) {
			return ht.get(key);
		}
		return 0;
	}

	public static int tryGet(Hashtable<String, Integer> ht, String key) {
		if (null != ht.get(key)) {
			return ht.get(key);
		}
		return 0;
	}

	public static int tryGetI(Hashtable<String, Integer> ht, String key) {
		if (null != ht.get(key)) {
			return ht.get(key);
		}
		return 0;
	}


	public static BufferedReader newReader(String file) {
		try {
			return new BufferedReader(new FileReader(file));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			System.err.println("fail to read file "+file);
		}
		return null;
	}



	public static String Array2String(Object[] a) {
		String res = "[";
		for (int i = 0; i < a.length; i++) {
			res += " " + a[i];
		}
		return res + " ]";
	}

	public static String Array2String(int[] a) {
		String res = "[";
		for (int i = 0; i < a.length; i++) {
			res += " " + a[i];
		}
		return res + " ]";
	}

	public static String Array2String(double[] a) {
		String res = "[";
		for (int i = 0; i < a.length; i++) {
			res += " " + a[i];
		}
		return res + " ]";
	}


	static int[] intSas2A(String s) {
		String[] sa = s.split(" ");
		int[] res = new int[sa.length - 2];
		for (int i = 1; i < sa.length - 1; i++) {
			res[i - 1] = Integer.parseInt(sa[i]);
		}
		return res;
	}

	static double[] doubleSas2A(String s) {
		String[] sa = s.split(" ");
		double[] res = new double[sa.length - 2];
		for (int i = 1; i < sa.length - 1; i++) {
			res[i - 1] = Double.parseDouble(sa[i]);
		}
		return res;
	}

	
	public static String trimBeginAndEnd(String s) {
		if (null == s || s.equals(""))
			return s;
		int i = 0;
		for (; i < s.length() && s.charAt(i) == ' '; i++)
			;
		if (i == s.length())
			return "";
		return s.substring(i).trim();
	}



	public static HashSet<String> loadHashSet(String f) throws IOException {
		BufferedReader r = U.newReader(f);
		HashSet<String> res = new HashSet<String>();
		while (true) {
			String l = r.readLine();
			if (null == l)
				break;
			res.add(l);
		}
		r.close();
		return res;
	}

	public static int[] arrayAdd(int[] a, int i) {
		int[] res = new int[a.length + 1];
		for (int j = 0; j < a.length; j++) {
			res[j] = a[j];
		}
		res[res.length - 1] = i;
		return res;
	}

	public static double[] arrayAdd(double[] a, double i) {
		double[] res = new double[a.length + 1];
		for (int j = 0; j < a.length; j++) {
			res[j] = a[j];
		}
		res[res.length - 1] = i;
		return res;
	}

	public static int getMaxIndex(int[] a) {
		int max = -Integer.MAX_VALUE;
		int index = -1;
		for (int i = 0; i < a.length; i++) {
			if (a[i] > max) {
				max = a[i];
				index = i;
			}

		}
		return index;
	}

	// ascending order
	public static int[] sort(int[] a) {
		if (a == null)
			return null;
		int[] res = new int[a.length];
		for (int i = res.length - 1; i >= 0; i--) {
			int j = U.getMaxIndex(a);
			res[i] = a[j];
			a[j] = -Integer.MAX_VALUE;
		}
		return res;
	}

	public static Vector<String> setMinus(Set<String> set1,
			Set<String> _set2) {
		Vector<String> res = new Vector<String>();
		for (String s1 : set1) {
			if (!_set2.contains(s1))
				res.add(s1);
		}
		return res;
	}

	@SuppressWarnings("unchecked")
	public static void setAdd(Set featureSet, Set featureSet2) {
		for (Object o : featureSet2)
			featureSet.add(o);

	}

	
	public static HashSet<String> vector2Set(Vector<String> v){
		HashSet<String> set =new HashSet<String>();
		for(String f:v)
			set.add(f);
		return set;
	}


	public static HashSet<String> vector2HashSet(Vector<String> v) {
		HashSet<String> res =new HashSet<String>();
		for(String f:v)
			res.add(f);
		return res;
	}

	public static String[] set2Sa(Set<String> labels) {
		String[] res=new String[labels.size()];
		int i=0;
		for(String o:labels){
			res[i]=o;
			i++;
		}
		return res;
	}
	
	public static boolean StrIsEmpty(String s){
		if(null== s ||s.trim().equals(""))
			return true;
		return false;
	}

	public static String getWholeTextFromFile(String file){
		String res ="";
		BufferedReader br =U.newReader(file);
		try {
			while(true){
				String l =br.readLine();
				if(null==l) break;
				res+=l+"\n";
			}
			
			br.close();
		} catch (IOException e) {
			System.err.println("file to read file: "+file);
			e.printStackTrace();
		}
		if(res.length()>0)
			res=res.substring(0,res.length()-1);
		return res;
	}
	
	public static void saveStringToFile(String str,String file){
		BufferedWriter bw =U.newWriter(file);
		try {
			bw.write(str);
			bw.close();
		} catch (IOException e) {
			System.err.println("file to write file: "+file);
			e.printStackTrace();
		}
	}
	
	
}
